package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private  JedisPool jedisPool;
//    预约时发送验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
//        先生成一个随机的四位数验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
//        通过腾讯云服务发送一个验证码到指定的手机号
        String[] telephones = {"+86"+telephone};
        String[] codes = {code};
        SMSUtils.sendShortMessage(SMSUtils.ORDER_CODE,telephones,codes);
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER, 300, code);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);


    }

    //    登录发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
//        先生成一个随机的四位数验证码
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
//        通过腾讯云服务发送一个验证码到指定的手机号
        String[] telephones = {"+86"+telephone};
        String[] codes = {code};
        SMSUtils.sendShortMessage(SMSUtils.LOGIN_CODE,telephones,codes);
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN, 300, code);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);


    }

}
